import cv2
import numpy as np
import os
from glob import glob

# 生成字符画
arr = ["@", "W", "#", "$", "O", "E", "X", "C", "[", "(", "/", "?", "=", "^", "~", "_", ".", " "]
arr_len = arr.__len__() - 1


def trans_to_charImg(path):
    # 读图
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img = cv2.bitwise_not(img)

    # 初始化与原图一样尺寸的黑色图片
    h = np.size(img, 0)
    w = np.size(img, 1)
    res = np.ndarray([h, w])
    font = cv2.FONT_HERSHEY_SIMPLEX
    for i in range(0, h, 10):
        for j in range(0, w, 10):
            # 获取灰度值对应字符
            t = arr[round(arr_len - img[i, j] / 255 * arr_len)]
            # 绘制字符
            cv2.putText(res, t, (j, i), font, 0.5, color=(255, 255, 255))
    # 保存结果图
    save_filename = '%s.png' % (os.path.basename(filename).split('.')[0])
    print(save_filename)
    # cv2.imshow(save_filename, res)
    # cv2.waitKey(0)
    cv2.imwrite('./images/charGrayFrames/' + save_filename, res)


if __name__ == "__main__":
    # if not os.path.exists('edges'):
    #     os.makedirs('edges')
    file_list = glob('images/frames/*.png')
    for filename in file_list:
        trans_to_charImg(filename)